import { glUtils } from "../resources/lib/GLUtils";




const { ccclass, property } = cc._decorator;

//export let SceneMgr = null;

@ccclass
class SceneMgr extends cc.Component {
	@property(cc.Prefab)
	marqueePrefeb: cc.Prefab = null;
	@property(cc.Prefab)
	screenClickPrefeb: cc.Prefab = null;

	@property(cc.Node)
	game: cc.Node = null;
	@property(cc.Node)
	hall: cc.Node = null;

	@property(cc.Node)
	login: cc.Node = null;

	@property(cc.Node)
	marquee: cc.Node = null;

	@property(cc.Node)
	screenClick: cc.Node = null;

	@property(cc.Node)
	danMu: cc.Node = null;
	@property(cc.Prefab)
	danMuPrefab: cc.Prefab = null;

	curScene: string = "";
	curName: string = "";

	marqueeSrc = null;
	marqueeTime = 0;
	showloadingevent = null
	hideloadingevent = null

	start() {
		// SceneMgr = this;
		cc.vv.sceneMgr = this;
		this.curScene = "";
		this.curName = "";

		this.openScene("login");


		this.initMarquee();
		this.initDanMu()
		//this.initScreenClick();

		// test
		//this.test();
	}

	initOnce() {
		this.curScene = "login";
	}

	initDanMu(){
		let node = cc.instantiate(this.danMuPrefab);
		this.danMu.addChild(node, 100);
	}


	initMarquee() {
		var node = cc.instantiate(this.marqueePrefeb);
		this.marquee.addChild(node, 100);
		this.marqueeSrc = node.getComponent("Marquee");
		cc.vv.MarqueeDataModel.init(this.marqueeSrc);
		cc.vv.MarqueeDataModel.close();
	}
	getMarquee() {
		return this.marqueeSrc;
	}


	initScreenClick() {
		var node = cc.instantiate(this.screenClickPrefeb);
		node.parent = this.screenClick
	}


	update(dt) { }

	getPath(name: string) {
		var _path = "";

		if (name == "login") {
			_path = "Login/Login";
		} else if (name == "hall") {
			_path = "Hall/hall/Hall";
		} else {
			_path = "Game/" + name + "/" + name;
		}
		return _path;
	}

	openScene(name, data = null, callBack = null) {
		var _path = "";
		var _rootName = name;

		//从游戏中退出到大厅    横竖屏切换 切换到竖屏
		if (cc.vv.sceneName == "Game" && name == "hall") {
			// this.bgloading.active = true;
			// cc.vv.UIMgr.closeAll();
			cc.vv.audioUtil.stopMusic();

			// //游戏外设置帧率45
			// cc.game.setFrameRate(45);
			// cc.vv.GLUtils.changeCanvasToVertical();

			data = data || {};
			data.lastScene = "Game";
			if(name == "hall"){
				cc.vv.sceneName = "Hall";
				cc.vv.eventCustom.emit("ExitShowGame")  //这里游戏关闭了  要关闭 大厅翻牌机列表 drawcall高
			}
			_path = this.getPath(name);
			cc.vv.UIMgr.open(_path, this[_rootName], function () {
				// this.bgloading.active = false;
				if (this.curScene != _rootName) {
					this.close(this.curName, cc.vv.UIMgr.closeType.destory);
				}
				this.curScene = _rootName;
				this.curName = name;
				if (callBack) {
					callBack();
				}
			}.bind(this), data);
			return;
		}

		if (name != "login" && name != "hall") {
			if (cc.vv.sceneName == "Game") {
				return;
			}
			if (!this.node) {
				return;
			}
			//游戏中设置帧率60
			cc.game.setFrameRate(60);
			// this.bgloading.active = true;
			// cc.vv.UIMgr.closeAll();
			_rootName = "game";
			cc.vv.sceneName = "Game";
			console.log("1111")
			data = data || {};
			data.platform = cc.vv.GLConfig.platform;
			if (data.gamemode === undefined) {
				data.gamemode = cc.vv.GLDefine.gameMode.match;
			}
			cc.vv.GLUtils.changeCanvasToHorizontal()
			_path = this.getPath(name);
			cc.vv.UIMgr.open(_path, this[_rootName], function () {
				// if (this.curScene != _rootName) {
				// 	this.close(this.curName, cc.vv.UIMgr.closeType.destory);
				// }
				// this.bgloading.active = false;
				cc.vv.eventCustom.emit("StartShowGame")  //这里游戏打开了   要关闭 大厅翻牌机列表 drawcall高
				this.curScene = _rootName;
				this.curName = name; //_path;
				if (callBack) {
					callBack();
				}
			}.bind(this), data);
		} else {
			if (name == "hall") {
				data = data || {};
				data.lastScene = this.curName;
			}
			//游戏外设置帧率45
			//cc.game.setFrameRate(45);
			// this.bgloading.active = true;
			// cc.vv.UIMgr.closeAll();
			cc.vv.GLUtils.changeCanvasToVertical();
			_path = this.getPath(name);

			cc.vv.UIMgr.open(_path, this[_rootName], function () {
				// this.bgloading.active = false;
				if (this.curScene != _rootName) {
					this.close(this.curName, cc.vv.UIMgr.closeType.destory);
				}
				this.curScene = _rootName;
				this.curName = name; //_path;
				if (callBack) {
					callBack();
				}
			}.bind(this), data);

			//
			console.log("SceneMgr openScene curName = " + this.curName);
		}

	}

	exitGame() {
		var games = this.game.children;
		for (var i = 0; i < games.length; i++) {
			games[i].destroy();
		}
		cc.vv.session.setCurGame("Hall");
		this.openScene("Hall");
	}

	close(name, type) {
		let _path = "";
		_path = this.getPath(name);

		cc.vv.UIMgr.close(_path, type);
		if (type != cc.vv.UIMgr.closeType.hide) {
			this.curScene = "";
			this.curName = "";
		}

		console.log("SceneMgr close curName = " + this.curName);
	}

	//test
	test() {

	}
}
export let sceneMgr = new SceneMgr();
